import express from 'express';
import { TeacherController } from '../controllers/teacherController.js';
import { authenticateToken } from '../middleware/auth.js';
import { requireAdmin, canManageTeacherMiddleware, attachUserInfo } from '../middleware/rbac.js';

console.log('=== 开始加载老师路由 ===');

// 调试导入
try {
  console.log('1. 尝试导入 TeacherController...');
  console.log('TeacherController import:', TeacherController);
} catch (error) {
  console.error('导入 TeacherController 失败:', error);
  process.exit(1);
}

const router = express.Router();

// 调试实例化
try {
  console.log('2. 尝试实例化 TeacherController...');
  const teacherController = new TeacherController();
  console.log('teacherController 实例创建成功:', typeof teacherController);
  console.log('getFormOptions 方法:', typeof teacherController.getFormOptions);

  // 所有路由都需要认证
  router.use(authenticateToken);
  router.use(attachUserInfo);

  // 获取老师表单选项数据
  router.get('/form-options', teacherController.getFormOptions.bind(teacherController));

  // 获取老师列表
  router.get('/', teacherController.getTeachers.bind(teacherController));

  // 获取老师详情
  router.get('/:id', teacherController.getTeacherDetail.bind(teacherController));

  // 获取老师统计信息
  router.get('/stats/overview', teacherController.getTeacherStats.bind(teacherController));

  // 创建老师 - 仅管理员
  router.post('/', requireAdmin, teacherController.createTeacher.bind(teacherController));

  // 更新老师 - 只能编辑自己（管理员可以编辑所有）
  router.put('/:id', canManageTeacherMiddleware, teacherController.updateTeacher.bind(teacherController));

  // 删除老师 - 仅管理员
  router.delete('/:id', requireAdmin, teacherController.deleteTeacher.bind(teacherController));

  console.log('=== 老师路由加载完成 ===');
} catch (error) {
  console.error('实例化 TeacherController 失败:', error);
  console.error('错误堆栈:', error.stack);
  process.exit(1);
}

export default router;